package com.my.mymafengwo.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.smallstrong.mafengwopro.R;

/**
 * ***********************************************
 * author:ljz
 * time:2015/12/4 10:52
 * name:自定义的EditText控件(有三个自定义的属性hint，textSize，textColorHint)
 * step:
 * ***********************************************
 */
public class CustomEditText extends LinearLayout {
    private EditText etSearch;
    private ImageView ivClear;
    private Context context;
    private OnTextChangedListener onTextChangedListener;//设置回调接口
    private MyOnClickListener onClickListener;//设置点击回调接口

    public CustomEditText(Context context) {
        this(context, null);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
        // 将自定义的属性从属性集中抽取出来
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SearchView);
        String hint = ta.getString(R.styleable.SearchView_hint);
        float textSize = ta.getDimension(R.styleable.SearchView_textSize, 5);
        int textColorHint = ta.getColor(R.styleable.SearchView_textColorHint, Color.parseColor("#b0c6ce"));
        setHint(hint);
        setTextColor(textColorHint);
        setTextSize(textSize);
        ta.recycle();
    }

    /**
     * 动态设置属性的方法
     *
     * @param hint
     */
    public void setHint(String hint) {
        etSearch.setHint(hint);
    }

    /**
     * 动态设置属性的方法
     *
     * @param textColorHint
     */
    public void setTextColor(int textColorHint) {
        etSearch.setTextColor(textColorHint);
    }

    /**
     * 动态设置textSize属性的方法
     *
     * @param textSize
     */
    public void setTextSize(float textSize) {
        etSearch.setTextSize(textSize);
    }

    private void initView() {
        findViews();
        initData();
        registerListener();

    }

    public interface MyOnClickListener {
        void onClick(View v);
    }
    public void setOnClickListener2(MyOnClickListener onClickListener){
        this.onClickListener=onClickListener;
    }
    public interface OnTextChangedListener {
        void beforeTextChanged(CharSequence s, int start, int count, int after);

        void onTextChanged(CharSequence s, int start, int before, int count);

        void afterTextChanged(Editable s);
    }

    public void setOnTextChangedListener(OnTextChangedListener onTextChangedListener) {
        this.onTextChangedListener = onTextChangedListener;
    }

    private void registerListener() {
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (onTextChangedListener != null) {
                    onTextChangedListener.beforeTextChanged(s, start, count, after);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (onTextChangedListener != null) {
                    onTextChangedListener.onTextChanged(s, start, before, count);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                int textLength = etSearch.getText().length();
                if (textLength > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                } else {
                    ivClear.setVisibility(View.GONE);
                }
                if (onTextChangedListener != null) {
                    onTextChangedListener.afterTextChanged(s);
                }
            }
        });
        etSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListener!=null){
                    onClickListener.onClick(v);
                }
            }
        });
        ivClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
                ivClear.setVisibility(INVISIBLE);
            }
        });
        etSearch.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View arg0, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    Toast.makeText(context, "暂未开通此功能", Toast.LENGTH_LONG).show();
                }
                return false;
            }
        });
    }


    private void initData() {
    }

    private void findViews() {
        LayoutInflater.from(context).inflate(R.layout.custom_edittext, this, true);
        ivClear = (ImageView) findViewById(R.id.iv_clear);
        etSearch = (EditText) findViewById(R.id.et_search);
    }
}


